package com.ggh.jinjilive.ui.live.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemSellGoodsBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.live.SellGoodsMobile;

import java.util.List;

public class SellGoodsAdapter extends RecyclerView.Adapter<SellGoodsAdapter.ItemViewHolder> {


    private List<SellGoodsMobile> sellGoodsMobileList;
    public void setSellGoodsMobile(List<SellGoodsMobile> sellGoodsMobileList){
        this.sellGoodsMobileList = sellGoodsMobileList;
        notifyDataSetChanged();
    }




    private AddItemOnClickListener addItemOnClickListener;
    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemSellGoodsBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_sell_goods, parent, false);
        return new ItemViewHolder(binding);
    }

    public void onBindViewHolder(@NonNull ItemViewHolder holder, final int position) {
        SellGoodsMobile sellGoodsMobile = sellGoodsMobileList.get(position);
        if(null != sellGoodsMobile){
            //头像
            Glide.with(holder.binding.imgSellGoods).load(sellGoodsMobile.getImg()).into(holder.binding.imgSellGoods);
            //商品名称
            holder.binding.tvName.setText(sellGoodsMobile.getName());
            //价格
            holder.binding.tvPrice.setText("￥"+sellGoodsMobile.getPrice());
            //是否带货中
            if(sellGoodsMobile.getS_status() == 0){
                holder.binding.tvSellGoods.setVisibility(View.GONE);
                holder.binding.tvSStatus.setVisibility(View.GONE);
            }else {
                holder.binding.tvSellGoods.setVisibility(View.VISIBLE);
                holder.binding.tvSStatus.setVisibility(View.VISIBLE);
            }
            //点击
            holder.binding.llMyTaskList.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(null != addItemOnClickListener){
                        addItemOnClickListener.onSkipClicked(0,sellGoodsMobile);
                    }
                }
            });
            //结束
            holder.binding.tvSellGoods.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(null != addItemOnClickListener){
                        addItemOnClickListener.onSkipClicked(1,sellGoodsMobile);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return null == sellGoodsMobileList ? 0 : sellGoodsMobileList.size();
    }


    class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemSellGoodsBinding binding;
        public ItemViewHolder(@NonNull ItemSellGoodsBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}